const express = require('express')
const { createServer } = require("http");
const router = require('./router')
const morgan = require('morgan')
const cors = require('cors')
const socket = require('./socket')
const path = require('path')


const PORT = process.env.PORT || 3500
// const errHandler = require('./middleware/error-handler')
// require('./model')


const app = express()
const httpServer = createServer(app);

app.use(express.static(path.join(__dirname,'/public')))

socket(httpServer)

app.use(cors())

app.use(morgan('dev'))
// 解析applocaltion/json 请求体
app.use(express.json())
app.use(express.urlencoded({extended: false}))
// 跨域处理

app.use('/api',router)
app.get('/',(req,res)=>{
    res.sendFile(path.join(__dirname,'public/index.html'))
})

// 错误处理
// app.use(errHandler())

httpServer.listen(PORT,()=>{
    console.log(`Server is running at http://localhost:${PORT}`);
})